<?php

/**
 * 商城
 */
class Api_Shop extends PhalApi_Api
{
	
	public function getRules()
	{
		return array(
			
			'getBond' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
			),
			
			'deductBond' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'time'  => array('name' => 'time', 'type' => 'string', 'desc' => '当前时间戳'),
				'sign'  => array('name' => 'sign', 'type' => 'string', 'desc' => '签名'),
			),
			
			'shopApply' => array(
				'uid'               => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'             => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'username'          => array('name' => 'username', 'type' => 'string', 'desc' => '姓名'),
				'cardno'            => array('name' => 'cardno', 'type' => 'string', 'desc' => '身份证号'),
				'classid'           => array('name' => 'classid', 'type' => 'string', 'desc' => '经营类目'),
				'contact'           => array('name' => 'contact', 'type' => 'string', 'desc' => '经营联系人'),
				'country_code'      => array('name' => 'country_code', 'type' => 'int', 'desc' => '国家代号', 'default' => '86'),
				'phone'             => array('name' => 'phone', 'type' => 'string', 'desc' => '手机号'),
				'province'          => array('name' => 'province', 'type' => 'string', 'desc' => '省份'),
				'city'              => array('name' => 'city', 'type' => 'string', 'desc' => '市'),
				'area'              => array('name' => 'area', 'type' => 'string', 'desc' => '地区'),
				'address'           => array('name' => 'address', 'type' => 'string', 'desc' => '详细地址'),
				'service_phone'     => array('name' => 'service_phone', 'type' => 'string', 'desc' => '客服电话'),
				'receiver'          => array('name' => 'receiver', 'type' => 'string', 'desc' => '收货人'),
				'receiver_phone'    => array('name' => 'receiver_phone', 'type' => 'string', 'desc' => '收货人手机号'),
				'receiver_province' => array('name' => 'receiver_province', 'type' => 'string', 'desc' => '收货人省份'),
				'receiver_city'     => array('name' => 'receiver_city', 'type' => 'string', 'desc' => '收货人市'),
				'receiver_area'     => array('name' => 'receiver_area', 'type' => 'string', 'desc' => '收货人地区'),
				'receiver_address'  => array('name' => 'receiver_address', 'type' => 'string', 'desc' => '收货人详细地址'),
				'certificate'       => array('name' => 'certificate', 'type' => 'string', 'desc' => '营业执照'),
				'other'             => array('name' => 'other', 'type' => 'string', 'desc' => '其他证件'),
			
			),
			
			'getShopApplyInfo' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户Token'),
				'time'  => array('name' => 'time', 'type' => 'string', 'desc' => '时间戳'),
				'sign'  => array('name' => 'sign', 'type' => 'string', 'desc' => '签名'),
			),
			
			'setSale' => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'goodsid' => array('name' => 'goodsid', 'type' => 'int', 'desc' => '商品ID'),
				'issale'  => array('name' => 'issale', 'type' => 'int', 'desc' => '在售状态，0否1是'),
			),
			
			'getShop' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'touid' => array('name' => 'touid', 'type' => 'int', 'desc' => '对方ID'),
				'p'     => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页码'),
			),
			
			'getSale' => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'liveuid' => array('name' => 'liveuid', 'type' => 'int', 'desc' => '主播ID'),
				'p'       => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页码'),
			),
			
			
			'getShopInfo' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'touid' => array('name' => 'touid', 'type' => 'int', 'desc' => '对方ID'),
			),
			
			'getGoodsInfo' => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'goodsid' => array('name' => 'goodsid', 'type' => 'int', 'desc' => '商品ID'),
			),
			
			'getGoodsCommentList' => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'goodsid' => array('name' => 'goodsid', 'type' => 'int', 'desc' => '商品ID'),
				'type'    => array('name' => 'type', 'type' => 'string', 'desc' => '评论类型 all 全部 img 有图 video 有视频 append 追加'),
				'p'       => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页码'),
			),
			
			'searchShopGoods'          => array(
				'uid'      => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'    => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'keywords' => array('name' => 'keywords', 'type' => 'string', 'desc' => '关键词'),
				'p'        => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页码'),
			),
			'setCollect'               => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'goodsid' => array('name' => 'goodsid', 'type' => 'int', 'require' => true, 'desc' => '商品ID'),
			),
			'getGoodsCollect'          => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'p'     => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页码'),
			
			),
			'getBusinessCategory'      => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
			
			),
			'getApplyBusinessCategory' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
			
			),
			'applyBusinessCategory'    => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
				'classid' => array('name' => 'classid', 'type' => 'string', 'desc' => '经营类目'),
			
			),
			'getGoodExistence'         => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'require' => true, 'desc' => '用户ID'),
				'goodsid' => array('name' => 'goodsid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '商品ID'),
			),
			
			'setPlatformGoodsSale' => array(
				'uid'     => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'   => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'goodsid' => array('name' => 'goodsid', 'type' => 'int', 'desc' => '商品ID'),
				'issale'  => array('name' => 'issale', 'type' => 'int', 'desc' => '在售状态，0否1是'),
			),
			
			'searchOnsalePlatformGoods' => array(
				'uid'      => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token'    => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'keywords' => array('name' => 'keywords', 'type' => 'string', 'desc' => '关键词'),
				'p'        => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页码'),
			),
			
			'getOnsalePlatformGoods' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'touid' => array('name' => 'touid', 'type' => 'int', 'desc' => '对方ID'),
				'p'     => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页码'),
			),
			'delGoodsCollect'        => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'ids'   => array('name' => 'ids', 'type' => 'string', 'desc' => '商品收藏记录id列表'),
			),
		
		);
	}
	
	/**
	 *获取保证金
	 * @desc 用于获取保证金设置数和用户是否缴纳保证金
	 * @return int code 状态码，0表示成功
	 * @return array info 状态码，0表示成功
	 * @return array info[0].shop_bond 后台设置的保证金金额
	 * @return array info[0].bond_status 用户是否缴纳保证金
	 * @return string msg 提示信息
	 */
	public function getBond()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$configpri = getConfigPri();
		$shop_bond = $configpri['shop_bond'];
		$domain    = new Domain_Shop();
		$res       = $domain->getBond($uid);
		//没有缴纳保证金
		if($res == -1){
			$info['bond_status'] = '0';
		}
		//保证金已退回
		if($res == 1){
			$info['bond_status'] = '0';
		}
		//保证金已缴纳/已处理
		if($res == 2){
			$info['bond_status'] = '1';
		}
		$info['shop_bond'] = $shop_bond;
		
		return $this->success(['0' => $info]);
	}
	
	
	/**
	 *缴纳保证金
	 * @desc 用于用户缴纳保证金
	 * @return int code 状态码，0表示成功
	 * @return array info 状态码，0表示成功
	 * @return string msg 提示信息
	 */
	public function deductBond()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$time  = checkNull($this->time);
		$sign  = checkNull($this->sign);
		$now   = time();
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		if(!$time){
			return $this->fail('参数错误,请重试!', 1001);
		}
		if($now - $time > 300){
			return $this->fail('参数错误!', 1001);
		}
		if(!$sign){
			return $this->fail('参数错误,请重试!', 1001);
		}
		
		$checkdata = array('uid' => $uid, 'token' => $token, 'time' => $time,);
		$issign    = checkSign($checkdata, $sign);
		if(!$issign){
			return $this->fail('签名错误!', 1001);
		}
		
		$configpri = getConfigPri();
		$shop_bond = isset($configpri['shop_bond']) ? $configpri['shop_bond'] : '';
		if(!$shop_bond){
			return $this->fail('保证金设置无法缴纳!', 1002);
		}
		
		$domain = new Domain_Shop();
		$result = $domain->deductBond($uid, $shop_bond);
		if($result == 1001){
			return $this->fail('已缴纳保证金!', 1003);
		}
		if($result == 1002){
			return $this->fail('余额不足!', 1004);
		}
		if($result == 1003){
			return $this->fail('保证金缴纳失败!', 1005);
		}
		
		return $this->success([], '保证金缴纳成功！');
	}
	
	/**
	 * 获取一级商品分类
	 * @desc 用于获取一级商品分类
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return array info[].gc_id 商品分类id
	 * @return array info[].gc_name 商品分类名称
	 * @return string msg 提示信息
	 */
	public function getOneGoodsClass()
	{
		$domain = new Domain_Shop();
		$list   = $domain->getOneGoodsClass();
		
		return $this->success($list);
	}
	
	
	/**
	 * 申请店铺
	 * @desc 用于申请店铺
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function shopApply()
	{
		$rs = array('code' => 0, 'msg' => '店铺申请成功', 'info' => array());
		
		$uid               = checkNull($this->uid);
		$token             = checkNull($this->token);
		$username          = checkNull($this->username);
		$cardno            = checkNull($this->cardno);
		$classid           = checkNull($this->classid);
		$contact           = checkNull($this->contact);
		$country_code      = checkNull($this->country_code);
		$phone             = checkNull($this->phone);
		$province          = checkNull($this->province);
		$city              = checkNull($this->city);
		$area              = checkNull($this->area);
		$address           = checkNull($this->address);
		$service_phone     = checkNull($this->service_phone);
		$receiver          = checkNull($this->receiver);
		$receiver_phone    = checkNull($this->receiver_phone);
		$receiver_province = checkNull($this->receiver_province);
		$receiver_city     = checkNull($this->receiver_city);
		$receiver_area     = checkNull($this->receiver_area);
		$receiver_address  = checkNull($this->receiver_address);
		$certificate       = checkNull($this->certificate);
		$other             = checkNull($this->other);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		//判断用户是否实名认证
		$isauth = isAuth($uid);
		if(!$isauth){
			return $this->fail('请先进行实名认证!', 1001);
		}
		if(!$username){
			return $this->fail('请填写姓名!', 1001);
		}
		if(mb_strlen($username) > 20){
			return $this->fail('姓名长度不能超过20个字!', 1001);
		}
		if(!$cardno){
			return $this->fail('请填写身份证号!', 1001);
		}
		if(!isCreditNo($cardno)){
			return $this->fail('身份证号不合法!', 1001);
		}
		if(!$classid){
			return $this->fail('请选择经营类目!', 1001);
		}
		
		$classid_arr = explode(",", $classid);
		$domain      = new Domain_Shop();
		$class_list  = $domain->getOneGoodsClass();
		$is_exist    = 1;
		$gc_ids      = array_column($class_list, "gc_id");
		foreach($classid_arr as $k => $v){
			if(!in_array($v, $gc_ids)){
				$is_exist = 0;
				break;
			}
		}
		
		if(!$is_exist){
			return $this->fail('主营类目有误!', 1001);
		}
		if(!$contact){
			return $this->fail('请填写经营者联系人!', 1001);
		}
		if(mb_strlen($contact) > 20){
			return $this->fail('经营者联系人不能超过20个字!', 1001);
		}
		if(!$phone){
			return $this->fail('请填写经营者手机号!', 1001);
		}
		if(!checkMobile($phone)){
			return $this->fail('手机号码错误!', 1001);
		}
		if(!$province){
			return $this->fail('请选择所在省份!', 1001);
		}
		if(!$city){
			return $this->fail('请选择所在市!', 1001);
		}
		if(!$area){
			return $this->fail('请选择所在地区!', 1001);
		}
		if(!$address){
			return $this->fail('请填写详细地址!', 1001);
		}
		if(mb_strlen($address) > 50){
			return $this->fail('详细地址必须在50字以内!', 1001);
		}
		if($service_phone){
			$checkmobile = checkMobile($service_phone);
			if(!$checkmobile){
				return $this->fail('客服电话错误!', 1001);
			}
		} else{
			$service_phone = $phone;
		}
		if($receiver){
			if(mb_strlen($receiver) > 20){
				return $this->fail('收货人姓名长度不能超过20个字!', 1001);
			}
		} else{
			$receiver = $username;
		}
		
		if($receiver_phone){
			$checkmobile = checkMobile($receiver_phone);
			if(!$checkmobile){
				return $this->fail('退货电话错误!', 1001);
			}
		} else{
			$receiver_phone = $phone;
		}
		if(!$receiver_province){
			$receiver_province = $province;
		}
		if(!$receiver_city){
			$receiver_city = $city;
		}
		if(!$receiver_area){
			$receiver_area = $area;
		}
		if(!$receiver_address){
			$receiver_address = $address;
		}
		if(!$certificate){
			return $this->fail('请上传营业执照!', 1001);
		}
		if(!$other){
			return $this->fail('请上传其他证件!', 1001);
		}
		
		//判断保证金是否缴纳
		$bond_res = $domain->getBond($uid);
		if($bond_res == -1 || $bond_res == 1){ //没有缴纳保证金
			return $this->fail('请缴纳保证金!', 1001);
		}
		
		//判断店铺审核状态
		$apply_info   = $domain->getShopApplyInfo($uid);
		$apply_status = $apply_info['apply_status']; //-1 无审核记录 0 审核中 1 审核通过 2 审核拒绝
		if($apply_status == 0){
			return $this->fail('店铺审核中,请耐心等待!', 1001);
		}
		if($apply_status == 1){
			return $this->fail('店铺已审核通过!', 1001);
		}
		
		$data = array(
			'username'          => $username,
			'cardno'            => $cardno,
			'contact'           => $contact,
			'country_code'      => $country_code,
			'phone'             => $phone,
			'province'          => $province,
			'city'              => $city,
			'area'              => $area,
			'address'           => $address,
			'service_phone'     => $service_phone,
			'receiver'          => $receiver,
			'receiver_phone'    => $receiver_phone,
			'receiver_province' => $receiver_province,
			'receiver_city'     => $receiver_city,
			'receiver_area'     => $receiver_area,
			'receiver_address'  => $receiver_address,
			'certificate'       => $certificate,
			'other'             => $other,
			'status'            => 1,
		);
		//无审核记录
		if($apply_status == -1){
			$data['uid']     = $uid;
			$data['addtime'] = time();
		}
		//被拒绝
		if($apply_status == 2){
			$data['uptime'] = time();
		}
		
		$configpri         = getConfigPri();
		$shop_switch       = $configpri['show_switch'];
		$shoporder_percent = $configpri['shoporder_percent'];
		
		$data['order_percent'] = isset($shoporder_percent) ? $shoporder_percent : 0; //订单抽成比例
		if($shop_switch){
			$data['status'] = 0;
		}
		$res = $domain->shopApply($uid, $data, $apply_status, $classid_arr);
		if($res == 1001){
			return $this->fail('店铺审核提交失败!', 1002);
		}
		
		return $rs;
	}
	
	/**
	 * 用户获取店铺申请信息
	 * @desc 用于 用户获取店铺申请信息[只有在店铺审核状态为通过或拒绝时才返回信息]
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function getShopApplyInfo()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$time  = checkNull($this->time);
		$sign  = checkNull($this->sign);
		$now   = time();
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		if(!$time){
			return $this->fail('参数错误!', 1001);
		}
		if($now - $time > 300){
			return $this->fail('参数错误!', 1001);
		}
		if(!$sign){
			return $this->fail('参数错误!', 1001);
		}
		
		$checkdata = array('uid' => $uid, 'token' => $token, 'time' => $time);
		$issign    = checkSign($checkdata, $sign);
		if(!$issign){
			return $this->fail('签名错误!', 1001);
		}
		
		$domain = new Domain_Shop();
		$res    = $domain->getShopApplyInfo($uid);
		if($res['apply_status'] == -1){
			return $this->fail('未提交店铺审核!', 1001);
		}
		if($res['apply_status'] == 0){
			return $this->fail('店铺正在审核中!', 1001);
		}
		
		return $this->success(['0' => $res['apply_info']]);
	}
	
	
	/**
	 * 店铺信息(带商品列表)
	 * @desc 用于获取店铺信息(带商品列表)
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return object info[0].shop_info 店铺信息
	 * @return string info[0].shop_info.uid 店铺用户ID
	 * @return string info[0].shop_info.user_nickname 店铺用户昵称
	 * @return string info[0].shop_info.sale_nums 店铺总销量
	 * @return string info[0].shop_info.quality_points 店铺商品质量评分
	 * @return string info[0].shop_info.service_points 店铺服务质量评分
	 * @return string info[0].shop_info.express_points 店铺物流速度评分
	 * @return string info[0].shop_info.certificate 店铺营业执照
	 * @return string info[0].shop_info.other 店铺其他证件
	 * @return string info[0].shop_info.service_phone 店铺客服热线
	 * @return string info[0].shop_infos.province 店铺省份
	 * @return string info[0].shop_info.city 店铺城市
	 * @return string info[0].shop_info.area 店铺所在地区
	 * @return string info[0].shop_info.name 店铺名称
	 * @return string info[0].shop_info.avatar 商品封面
	 * @return string info[0].shop_info.composite_points 店铺综合评分
	 * @return string info[0].shop_info.goods_nums 店铺在售商品数量
	 * @return string info[0].shop_info.address_format 店铺地址格式化
	 * @return string info[0].shop_info.isattention 用户是否关注了店铺主播 0 否 1 是
	 * @return string info[0].list[].id 商品ID
	 * @return string info[0].list[].name 商品名
	 * @return string info[0].list[].thumb 商品封面
	 * @return string info[0].list[].sale_nums 商品销量
	 * @return string info[0].list[].price 现价
	 * @return string msg 提示信息
	 */
	public function getShop()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$touid = checkNull($this->touid);
		$p     = checkNull($this->p);
		if($uid > 0){
			$checkToken = checkToken($uid, $token);
			if($checkToken == 700){
				return $this->fail('您的登陆状态失效，请重新登陆!', $checkToken);
			}
		}
		
		$domain = new Domain_Shop();
		$info   = $domain->getShop($touid);
		if(!$info){
			return $this->fail('店铺不存在!', 1001);
		}
		
		$list = [];
		$nums = 0;
		if($touid > 1){
			$where = ['uid' => $touid, 'status' => 1];
			$list  = $domain->getGoodsList($where, $p);
			$nums  = $domain->countGoods($where);
		}
		
		//获取代售的平台商品数量
		$where1 = ['uid' => $touid, 'status' => 1];
		
		$info['isattention']         = isAttention($uid, $touid); //判断用户是否关注了店铺主播
		$info['platform_goods_nums'] = $domain->countPlatformSale($where1);
		$info['goods_nums']          = $nums; //店铺在售商品重新赋值
		
		return $this->success(['0' => ['shop_info' => $info, 'list' => $list]]);
	}
	
	
	/**
	 * 在售商品
	 * @desc 用于用户获取直播间在售商品
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[0].nums 总数
	 * @return array info[0].list 商品列表
	 * @return string info[0].list[].id 商品id
	 * @return string info[0].list[].name 商品名
	 * @return string info[0].list[].thumb 商品封面
	 * @return string info[0].list[].hits 查看次数
	 * @return string info[0].list[].price 商品价格
	 * @return string msg 提示信息
	 */
	public function getSale()
	{
		$uid     = checkNull($this->uid);
		$liveuid = checkNull($this->liveuid);
		$p       = checkNull($this->p);
		
		
		$domain = new Domain_Shop();
		$nums   = $domain->countSale($liveuid);
		
		$where1        = ['uid' => $liveuid, 'status' => 1, 'issale' => 1,];
		$platform_nums = $domain->countPlatformSale($where1);
		
		$where = ['uid=?' => $liveuid, 'status' => 1, 'issale' => 1,];
		$list  = $domain->getGoodsList($where, $p);
		
		$where1               = ['uid=?' => $liveuid, 'status' => 1,];
		$onsale_platform_list = $domain->onsalePlatformList($where1, $p); //加$p是为了适应小程序请求，其实是一次性返回数据
		
		$new_list = array_merge($onsale_platform_list, $list);
		$total    = $nums + $platform_nums;
		
		$info['nums'] = (string)$total;
		$info['list'] = $new_list;
		
		return $this->success(['0' => $info]);
	}
	
	/**
	 * 主播增删自己发布的在售商品
	 * @desc 用于主播增删自己发布的在售商品
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function setSale()
	{
		$uid     = checkNull($this->uid);
		$token   = checkNull($this->token);
		$goodsid = checkNull($this->goodsid);
		$issale  = checkNull($this->issale);
		
		if($uid < 0 || $token == '' || $goodsid < 0){
			return $this->fail('信息错误!', 1000);
		}
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		
		$domain = new Domain_Shop();
		$res    = $domain->setSale($uid, $goodsid, $issale);
		
		return $res;
	}
	
	
	/**
	 * 店铺信息(不带商品列表)
	 * @desc 用于获取店铺信息(不带商品列表)
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[0].uid 店铺用户ID
	 * @return string info[0].sale_nums 店铺总销量
	 * @return string info[0].quality_points 店铺商品质量评分
	 * @return string info[0].service_points 店铺服务质量评分
	 * @return string info[0].express_points 店铺物流速度评分
	 * @return string info[0].certificate 店铺营业执照
	 * @return string info[0].other 店铺其他证件
	 * @return string info[0].service_phone 店铺客服热线
	 * @return string info[0].province 店铺省份
	 * @return string info[0].city 店铺城市
	 * @return string info[0].area 店铺所在地区
	 * @return string info[0].name 店铺名称
	 * @return string info[0].name 店铺名称
	 * @return array info[0].avatar 商品封面
	 * @return array info[0].composite_points 店铺综合评分
	 * @return array info[0].goods_nums 店铺在售商品数量
	 * @return array info[0].address_format 店铺地址格式化
	 * @return string msg 提示信息
	 */
	public function getShopInfo()
	{
		$uid    = checkNull($this->uid);
		$touid  = checkNull($this->touid);
		$domain = new Domain_Shop();
		$info   = $domain->getShop($touid);
		if(!$info){
			return $this->fail('店铺不存在!', 1001);
		}
		
		//判断用户是否关注了店铺主播
		$info['isattention'] = isAttention($uid, $touid);
		
		
		return $this->success(['0' => $info]);
	}
	
	/**
	 * 获取商品详情
	 * @desc 用于获取商品详情
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return int info[0].id 商品id
	 * @return int info[0].uid 商品所属用户id
	 * @return string info[0].name 商品名称
	 * @return int info[0].one_classid 商品一级分类id
	 * @return int info[0].two_classid 商品二级分类id
	 * @return int info[0].three_classid 商品三级分类id
	 * @return string info[0].one_class_name 商品一级分类名称
	 * @return string info[0].two_class_name 商品二级分类名称
	 * @return string info[0].three_class_name 商品三级分类名称
	 * @return string info[0].video_url 商品视频地址
	 * @return string info[0].thumbs 商品封面图字符串
	 * @return string info[0].content 商品文字内容
	 * @return string info[0].pictures 商品内容图片字符串
	 * @return string info[0].specs 商品规格json字符串
	 * @return float info[0].postage 商品邮费
	 * @return string info[0].hits 商品访问量
	 * @return string info[0].status 商品状态 0审核中 1审核通过 -1商家下架 -2管理员下架
	 * @return string info[0].sale_nums 商品总销量
	 * @return string info[0].video_url_format 商品视频地址格式化
	 * @return array info[0].thumbs_format 商品封面格式化
	 * @return array info[0].pictures_format 商品内容图片格式化
	 * @return string info[0].comment_nums 商品评价数
	 * @return array info[0].shop_info 商品所在店铺信息
	 * @return string info[0].shop_info.name 商品所在店铺名称
	 * @return string info[0].shop_info.avatar 商品所在店铺头像
	 * @return string info[0].shop_info.sale_nums 商品所在店铺总销量
	 * @return string info[0].shop_info.quality_points 商品所在店铺商品质量评分
	 * @return string info[0].shop_info.service_points 商品所在店铺服务评分
	 * @return string info[0].shop_info.express_points 商品所在店铺物流服务评分
	 * @return string info[0].shop_info.isattention 用户是否关注了店铺主播 0 否 1 是
	 * @return array info[0].comment_lists 商品评价信息
	 * @return array info[0].is_sale_platform 用户是否代售了该商品
	 * @return string msg 提示信息
	 */
	public function getGoodsInfo()
	{
		$uid     = checkNull($this->uid);
		$token   = checkNull($this->token);
		$goodsid = checkNull($this->goodsid);
		if($goodsid < 0){
			return $this->fail('信息错误!', 1000);
		}
		if($uid > 0){
			$checkToken = checkToken($uid, $token);
			if($checkToken == 700){
				return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
			}
		}
		
		$domain = new Domain_Shop();
		$res    = $domain->getGoodsInfo($uid, $goodsid);
		
		return $res;
	}
	
	
	/**
	 * 根据不同类型获取商品评论列表
	 * @desc 用于根据不同类型获取商品评论列表
	 * @return int code 状态码，0表示成功
	 * @return string msg 提示信息
	 * @return array info 返回信息
	 * @return array info[0]['comment_lists'] 评论列表
	 * @return object info[0]['comment_lists'][].append_comment 追加评论信息
	 * @return array info[0]['type_nums'] 不同类型下的评论总数
	 */
	public function getGoodsCommentList()
	{
		$uid     = checkNull($this->uid);
		$token   = checkNull($this->token);
		$goodsid = checkNull($this->goodsid);
		$type    = checkNull($this->type);
		$p       = checkNull($this->p);
		
		//all 全部 img 有图 video 有视频 append 追加
		if($goodsid < 1 || !in_array($type, ['all', 'img', 'video', 'append'])){
			return $this->fail('信息错误!', 1000);
		}
		if($uid > 0){
			$checkToken = checkToken($uid, $token);
			if($checkToken == 700){
				return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
			}
		}
		
		$domain = new Domain_Shop();
		$res    = $domain->getGoodsCommentList($uid, $goodsid, $type, $p);
		
		return $this->success(['0' => $res]);
	}
	
	
	/**
	 * 用户发布的商品搜索
	 * @desc 用于用户发布的商品搜索
	 * @return int code 状态码，0表示成功
	 * @return string msg 提示信息
	 * @return array info 返回信息
	 * @return int info[0]['id'] 商品ID
	 * @return string info[0]['name'] 商品名称
	 * @return string info[0]['price'] 商品价格
	 * @return string info[0]['thumb'] 商品封面
	 */
	public function searchShopGoods()
	{
		$uid      = checkNull($this->uid);
		$token    = checkNull($this->token);
		$keywords = checkNull($this->keywords);
		$p        = checkNull($this->p);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		//判断用户是否开通了店铺
		$is_shop = checkShopIsPass($uid);
		if(!$is_shop){
			return $this->success();
		}
		
		$where = ['uid=?' => $uid, 'status' => 1];
		if($keywords != ''){
			$where['name like ?'] = '%'.$keywords.'%';
		}
		$domain = new Domain_Shop();
		$res    = $domain->getGoodsList($where, $p);
		
		return $this->success($res);
	}
	
	
	/**
	 * 收藏/取消收藏商品
	 * @desc 用于收藏/取消收藏商品
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[0].iscollect 收藏信息，0表示未收藏，1表示已收藏
	 * @return string msg 提示信息
	 */
	public function setCollect()
	{
		$uid     = checkNull($this->uid);
		$token   = checkNull($this->token);
		$goodsid = checkNull($this->goodsid);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Shop();
		$info   = $domain->setCollect($uid, $goodsid);
		
		return $info;
	}
	
	
	/**
	 * 收藏商品列表
	 * @desc 用于获取已收藏的商品
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function getGoodsCollect()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$p     = checkNull($this->p);
		
		$checkToken = checkToken($uid, $token);
		/*if($checkToken==700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}*/
		
		$domain = new Domain_Shop();
		$info   = $domain->getGoodsCollect($uid, $p);
		
		return $this->success($info);
	}
	
	/**
	 * 获取正在经营的一级商品分类
	 * @desc 用于获取一级商品分类
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return array info[].gc_id 商品分类id
	 * @return array info[].gc_name 商品分类名称
	 * @return string msg 提示信息
	 */
	public function getBusinessCategory()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Shop();
		$list   = $domain->getBusinessCategory($uid);
		
		return $this->success($list);
	}
	
	/**
	 * 获取正在申请的经营类目
	 * @desc 用于获取正在申请的经营类目
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return array info[].gc_id 商品分类id
	 * @return array info[].gc_name 商品分类名称
	 * @return string msg 提示信息
	 */
	public function getApplyBusinessCategory()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		$domain = new Domain_Shop();
		$apply  = $domain->getApplyBusinessCategory($uid);
		if(!$apply){
			$apply['status'] = '1';
		}
		
		return $this->success(['0' => $apply]);
	}
	
	
	/**
	 * 经营类目提交申请提交
	 * @desc 用于获取一级商品分类
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return array info[].gc_id 商品分类id
	 * @return array info[].gc_name 商品分类名称
	 * @return string msg 提示信息
	 */
	public function applyBusinessCategory()
	{
		$uid     = checkNull($this->uid);
		$token   = checkNull($this->token);
		$classid = checkNull($this->classid);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		if(!$classid){
			return $this->fail('请选择经营类目!', 1001);
		}
		
		$domain = new Domain_Shop();
		$info   = $domain->applyBusinessCategory($uid, $classid);
		if($info == 1001){
			return $this->fail('管理员正在飞速审核中，请勿重新提交!', 1001);
		} elseif(!$info){
			return $this->fail('系统繁忙，请稍后操作~!', 1002);
		}
		
		//判断后台是否开启免审
		$configpri       = getConfigPri();
		$category_switch = $configpri['show_category_switch'];
		$msg             = '管理员正在飞速审核中，请耐心等待~';
		if(!$category_switch){
			$msg = "申请成功";
		}
		
		return $this->success([], $msg);
	}
	
	
	/**
	 * 判断商品是否删除及下架
	 * @desc 用于判断商品是否删除及下架
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function getGoodExistence()
	{
		$uid     = checkNull($this->uid);
		$goodsid = checkNull($this->goodsid);
		$domain  = new Domain_Shop();
		$info    = $domain->getGoodExistence($uid, $goodsid);
		if(!$info){
			return $this->fail('商品不存在!', 1001);
		}
		
		return $this->success();
	}
	
	/**
	 * 主播增删直播间代售的平台商品
	 * @desc 用于主播增删直播间代售的平台商品
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string msg 提示信息
	 */
	public function setPlatformGoodsSale()
	{
		$uid     = checkNull($this->uid);
		$token   = checkNull($this->token);
		$goodsid = checkNull($this->goodsid);
		$issale  = checkNull($this->issale);
		if($uid < 0 || $token == '' || $goodsid < 0){
			return $this->fail('信息错误!', 1000);
		}
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		if(!in_array($issale, ['0', '1'])){
			return $this->fail('参数错误!', $checkToken);
		}
		
		$domain = new Domain_Shop();
		$res    = $domain->setPlatformGoodsSale($uid, $goodsid, $issale);
		if($res == 1001){
			return $this->fail('未代卖该商品!', 1001);
		}
		if($res == 1002){
			return $this->fail('该商品已下架!', 1002);
		}
		if($res == 1003){
			return $this->fail('操作失败!', 1003);
		}
		if($res == 1004){
			return $this->fail('该商品已经添加到了直播间!', 1004);
		}
		if($res == 1005){
			return $this->fail('该商品已经移除了直播间!', 1005);
		}
		
		return $this->success();
	}
	
	
	/**
	 * 用户代售平台的商品搜索
	 * @desc 用于用户代售平台的商品搜索
	 * @return int code 状态码，0表示成功
	 * @return string msg 提示信息
	 * @return array info 返回信息
	 * @return int info[0]['id'] 商品ID
	 * @return string info[0]['name'] 商品名称
	 * @return string info[0]['price'] 商品价格
	 * @return string info[0]['thumb'] 商品封面
	 */
	public function searchOnsalePlatformGoods()
	{
		$uid      = checkNull($this->uid);
		$token    = checkNull($this->token);
		$keywords = checkNull($this->keywords);
		$p        = checkNull($this->p);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		
		//判断用户是否开通了店铺
		$is_shop = checkShopIsPass($uid);
		if(!$is_shop){
			return $this->success();
		}
		
		$domain = new Domain_Shop();
		$res    = $domain->searchOnsalePlatformGoods($uid, $keywords, $p);
		
		return $this->success($res);
	}
	
	
	/**
	 * 获取店铺代售平台商品列表
	 * @desc 用于获取店铺代售平台商品列表
	 * @return int code 状态码，0表示成功
	 * @return string msg 提示信息
	 * @return array info 返回信息
	 * @return int info[].id 返回商品ID
	 * @return int info[].name 返回商品名称
	 * @return int info[].sale_nums 返回商品销量
	 * @return int info[].hits 返回商品访问量
	 * @return int info[].type 返回商品类型 2代表平台自营商品
	 * @return int info[].thumb 返回商品封面
	 * @return int info[].price 返回商品价格
	 */
	public function getOnsalePlatformGoods()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$touid = checkNull($this->touid);
		$p     = checkNull($this->p);
		if($touid < 0){
			return $this->fail('信息错误!', 1000);
		}
		if($uid > 0){
			$checkToken = checkToken($uid, $token);
			if($checkToken == 700){
				return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
			}
		}
		
		$domain    = new Domain_Shop();
		$shop_info = $domain->getShop($touid);
		if(!$shop_info){
			return $this->fail('店铺不存在!', 1001);
		}
		if($touid > 1){
			$where1        = ['uid' => $touid, 'status' => 1];
			$platform_nums = $domain->countPlatformSale($where1);
		} else{
			//平台自营
			$where         = ['uid' => $touid, 'status' => 1];
			$platform_nums = $domain->countGoods($where);
		}
		
		$shop_info['platform_goods_nums'] = $platform_nums;
		if($touid > 1){
			$list = $domain->getOnsalePlatformGoods($touid, $p);
		} else{
			$list = $domain->getGoodsList($where, $p);
		}
		
		return $this->success(['0' => ['shop_info' => $shop_info, 'list' => $list]]);
	}
	
	/**
	 * 删除商品收藏记录
	 * @desc 删除商品收藏记录
	 * @return int code 状态码，0表示成功
	 * @return string msg 提示信息
	 * @return array info 返回信息
	 */
	public function delGoodsCollect()
	{
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$ids   = checkNull($this->ids);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', $checkToken);
		}
		if(!$ids){
			return $this->fail('请选择收藏记录!', 1001);
		}
		
		$ids_arr = explode(',', $ids);
		$new_arr = array_filter($ids_arr);
		if(empty($new_arr)){
			return $this->fail('请选择收藏记录!', 1002);
		}
		
		$domain = new Domain_Shop();
		$res    = $domain->delGoodsCollect($uid, $new_arr);
		if(!$res){
			return $this->fail('清除失败!', 1003);
		}
		
		return $this->success([], '清除成功！');
	}
	
}
